package com.inforim.cognos.utils;

import org.apache.axis.AxisFault;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import com.inforim.cognos.CognosClient;
import com.inforim.cognos.plugins.CognosReportPlugin;

public class CognosHelper {

	static Log log = LogFactory.getLog(CognosHelper.class);

	public static CognosClient getClient() {
		String driver = System.getProperty("com.inforim.cognos.CognosClient",
				"com.inforim.c10.CognosClientImpl");
		System.setProperty("com.inforim.cognos.CognosClient","com.inforim.c10.CognosClientImpl");
    	System.setProperty("com.inforim.cognos.plugins.CognosReportPlugin", "com.inforim.c10.CognosReportPluginImpl");
		return getClient(driver);
	}

	public static CognosClient getClient(String driver) {
		CognosClient client = null;
		try {
			client = (CognosClient) Class.forName(driver).newInstance();
		} catch (ClassNotFoundException ex) {
			log.error("Cannot initial cognos client: " + driver
					+ ", class not found.");
		} catch (ClassCastException ex) {
			log.error("Cannot initial cognos client: " + driver
					+ ", class cast failed.");
		} catch (Exception ex) {
			log.error(
					"Cannot initial cognos client: " + driver + ", "
							+ ex.getMessage(), ex);
		}
		return client;
	}

	public static CognosClient getClient(String username, String password,
			String namespace, String passport, String dispatch, String version) {
		String driver = null;
		if (null == version) {
			driver = "com.inforim.c10.CognosClientImpl";
		} else {
			driver = "com.inforim.c84.CognosClientImpl";
		}
		CognosClient client = getClient(driver);
		if (null != client) {
			client.setUsername(username);
			client.setPassword(password);
			client.setNamespace(namespace);
			client.setDispatch(dispatch);
			client.setPassport(passport);
		}
		return client;
	}

	public static CognosReportPlugin getReportPlugin() {
		String driver = System.getProperty(
				"com.inforim.cognos.plugins.CognosReportPlugin",
				"com.inforim.c10.CognosReportPluginImpl");
		return getReportPlugin(driver);
	}

	public static CognosReportPlugin getReportPlugin(String driver) {
		CognosReportPlugin reportPlugin = null;
		try {
			reportPlugin = (CognosReportPlugin) Class.forName(driver).newInstance();
		} catch (ClassNotFoundException ex) {
			log.error("Cannot initial cognos repor plugin: " + driver
					+ ", class not found.");
		} catch (ClassCastException ex) {
			log.error("Cannot initial cognos repor plugin: " + driver
					+ ", class cast failed.");
		} catch (Exception ex) {
			log.error(
					"Cannot initial cognos repor plugin: " + driver + ", "
							+ ex.getMessage(), ex);
		}
		return reportPlugin;
	}

	public static String parseErrorDetails(Exception exception) {
		exception.printStackTrace();
		StringBuffer sb = new StringBuffer();
		if (exception instanceof AxisFault) {
			AxisFault fault = (AxisFault) exception;
			if (null != fault.getFaultReason()) {
				sb.append(fault.getFaultReason());
			}
			Element[] elems = fault.getFaultDetails();
			for (int i = 0; i < elems.length; i++) {
				String[] tags = new String[] { "serverity", "errorCode",
						"messageString" };
				for (int j = 0; j < tags.length; j++) {
					NodeList nl = elems[i].getElementsByTagName(tags[j]);
					if (null != nl && nl.getLength() > 0) {
						if (sb.length() > 0) {
							sb.append(", ");
						}
						// sb.append(nl.item(0).getNodeName());
						// sb.append(": ");
						sb.append(nl.item(0).getTextContent());
					}
				}
			}
		}
		return sb.toString();
	}

}
